package com.example.science.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
@ConfigurationProperties("huyi")
@Data
public class SMSUtil {
    private String url;
    private String account;
    private String password;
    public Integer sendCode(String mobile) throws Exception{
        //准备请求参数
        HashMap<String, Object> param = new HashMap<>();
        param.put("mobile",mobile);
        param.put("account",account);
        param.put("password",password);
        param.put("format","json");
        //String code=String.valueOf((int)(Math.random()*9000)+1000);
        Integer code  = (int)(Math.floor(Math.random() * 9000) + 1000);
        String content="您的验证码是：" + code + "。请不要把验证码泄露给其他人。";
        param.put("content",content);
        String body = HttpRequest.post(url).form(param).execute().body();
        if("2".equals(JSONUtil.parseObj(body).get("code").toString())){
            return code;
        }else {
            throw new Exception("短信发送失败");
        }
    }
}
